let flag_1 = false, flag_2 = false, flag_3 = false, flag_4 = false;

// 手机号判断
$('.phonenumber1').oninput = function () {
    const phonenumber = this.value;
    // 空
    if (/^.{0}$/.test(phonenumber)) {
        flag_1 = false;
        $('.phoneNumberSpan1').innerHTML = '不能为空';
        $('.phoneNumberSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 数字开头
    if (!/^\d/.test(phonenumber)) {
        flag_1 = false;
        $('.phoneNumberSpan1').innerHTML = '不能填写除数字外的字符';
        $('.phoneNumberSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 用户名长度
    if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(phonenumber)) {
        flag_1 = false;
        $('.phoneNumberSpan1').innerHTML = '号码不合法';
        $('.phoneNumberSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }

    flag_1 = true;
    $('.phoneNumberSpan1').innerHTML = '√';
    $('.phoneNumberSpan1').style.color = 'green';
    if (flag_1 && flag_2 && flag_3 && flag_4) {
        $('#lgb_lu').disabled = false;
    } else {
        $('#lgb_lu').disabled = true;
    }
}



$('.username1').oninput = function () {
    const username = this.value;
    // 空
    if (/^.{0}$/.test(username)) {
        flag_2 = false;
        $('.userSpan1').innerHTML = '不能为空';
        $('.userSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 数字开头
    if (/^\d/.test(username)) {
        flag_2 = false;
        $('.userSpan1').innerHTML = '数字不能开头';
        $('.userSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 非法   
    if (/\W/.test(username)) {
        flag_2 = false;
        $('.userSpan1').innerHTML = '非法';
        $('.userSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 用户名长度
    if (!/^\w{1,12}$/.test(username)) {
        flag_2 = false;
        $('.userSpan1').innerHTML = '用户名过长';
        $('.userSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }

    flag_2 = true;
    $('.userSpan1').innerHTML = '√';
    $('.userSpan1').style.color = 'green';
    if (flag_1 && flag_2 && flag_3 && flag_4) {
        $('#lgb_lu').disabled = false;
    } else {
        $('#lgb_lu').disabled = true;
    }
}

$('.password1').oninput = function () {
    const password = this.value;
    // 空
    if (/^.{0}$/.test(password)) {
        flag_3 = false;
        $('.passwordSpan1').innerHTML = '不能为空';
        $('.passwordSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 非法   
    if (/\W/.test(password)) {
        flag_3 = false;
        $('.passwordSpan1').innerHTML = '非法';
        $('.passwordSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    // 密码长度
    if (!/^\w{6,12}$/.test(password)) {
        flag_3 = false;
        $('.passwordSpan1').innerHTML = '密码长度请输6~12位';
        $('.passwordSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }

    flag_3 = true;
    $('.passwordSpan1').innerHTML = '√';
    $('.passwordSpan1').style.color = 'green';
    if (flag_1 && flag_2 && flag_3 && flag_4) {
        $('#lgb_lu').disabled = false;
    } else {
        $('#lgb_lu').disabled = true;
    }
}




$('.code1').oninput = function () {
    const codeValue = this.value;

    // 填写验证码
    //    验证码为空
    if (codeValue === '') {
        flag_4 = false;
        $('.codeSpan1').innerHTML = '请输入验证码';
        $('.codeSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }
    //    验证码输入判断--不区分大小写
    if (codeValue.toLowerCase() !== $('.showCode1').innerHTML.toLowerCase()) {
        flag_4 = false;
        $('.codeSpan1').innerHTML = '验证码输入错误';
        $('.codeSpan1').style.color = 'red';
        if (flag_1 && flag_2 && flag_3 && flag_4) {
            $('#lgb_lu').disabled = false;
        } else {
            $('#lgb_lu').disabled = true;
        }
        return
    }

    flag_4 = true;
    $('.codeSpan1').innerHTML = '√';
    $('.codeSpan1').style.color = 'green';
    if (flag_1 && flag_2 && flag_3 && flag_4) {
        $('#lgb_lu').disabled = false;
    } else {
        $('#lgb_lu').disabled = true;
    }
}



const numArr1 = [];
for (let i = 0; i < 10; i++) {
    numArr1.push(i + '')
}
console.log(numArr1);

const smallArr1 = [];
for (let i = 97; i <= 122; i++) {
    const str = String.fromCharCode(i);
    smallArr1.push(str)
}
console.log(smallArr1);

const bigArr1 = [];
for (let i = 65; i <= 90; i++) {
    const str = String.fromCharCode(i);
    bigArr1.push(str)
}
console.log(bigArr1);

const allArr1 = smallArr1.concat(numArr1, bigArr1);
console.log(allArr1);



//随机验证码
// 6为函数 suiJiCode 实参值
$('.showCode1').innerHTML = suiJiCode(6);


function suiJiCode(n) {
    // 给默认值
    n = n || 4;

    let res = '';
    res += numArr1[rand(0, numArr1.length)];
    res += smallArr1[rand(0, smallArr1.length)];
    res += bigArr1[rand(0, bigArr1.length)];
    for (let i = 0; i < n - 3; i++) {
        res += allArr1[rand(0, allArr1.length)];
    }
    // 字符串无法交换顺序
    let arr = res.split('');
    console.log(arr);
    // 交换次数至少n次
    for (let i = 0; i < arr.length; i++) {
        let index = rand(0, arr.length);

        [arr[i], arr[index]] = [arr[index], arr[i]];
        console.log(arr[i], arr[index]);
    }
    // console.log(arr);
    return arr.join('');

}


// 绑定验证码点击刷新事件
$('.showCode1').onclick = function () {
    $('.showCode1').innerHTML = suiJiCode(6);
    $('.code1').value = '';
    $('#lgb_lu').disabled = true;
    $('.codeSpan1').innerHTML = '请输入验证码';
    $('.codeSpan1').style.color = '#757575;'
}

function rand(min, max) {
    return parseInt(Math.random() * (max - min)) + min;
}






// $('#lgb_lu').onclick = async function () {
//     const phonenumber = $('.phonenumber1').value;
//     const username = $('.username1').value;
//     const password = $('.password1').value;
//     console.log(password);
//     const res = await login({
//         phonenumber,
//         username,
//         password
//     })
//     console.log(res);
//     const { status, msg } = res;
//     if (status) {
//         localStorage.setItem('login_user', username);
//         let url = location.search.split('?historyURL=')[1];

//         if (url) {
//             url = decodeURIComponent(url);
//             alert(msg);
//             location.href = url;
//         } else {
//             alert(msg);
//             location.href = './nike.html';
//         }
//     } else {
//         alert(msg);
//     }
// }
